package com.auto.code.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auto.code.dao.TemplateDAO;
import com.auto.code.domain.TemplateDO;
import com.auto.code.domain.TemplateDTO;
import com.auto.code.service.TemplateService;
import com.auto.code.util.MapUtils;
import com.auto.code.util.ObjectClone;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class TemplateServiceImpl implements TemplateService {
	private static final Logger logger = LoggerFactory.getLogger(TemplateServiceImpl.class);

	@Autowired
	private TemplateDAO templateDAO;

	@Override
	public int deleteById(Long id) {
		return templateDAO.deleteById(id);
	}

	@Override
	public int create(TemplateDTO record) {
		TemplateDO entity = new TemplateDO();
		BeanUtils.copyProperties(record, entity);
		entity.setType(entity.getType().toLowerCase());
		return templateDAO.insert(entity);
	}

	@Override
	public TemplateDO getById(Long id) {
		return templateDAO.getById(id);
	}

	@Override
	public int update(TemplateDTO record) {
		TemplateDO entity = new TemplateDO();
		BeanUtils.copyProperties(record, entity);
		entity.setType(entity.getType().toLowerCase());
		return templateDAO.update(entity);
	}

	@Override
	public PageInfo<TemplateDTO> queryPage(TemplateDTO templateDTO) {
		PageHelper.startPage(templateDTO.getPageNum(), templateDTO.getPageSize());
		List<TemplateDO> templateDOS = templateDAO.queryList(MapUtils.parseMap(templateDTO));
		List<TemplateDTO> templateDTOList = ObjectClone.cloneList(templateDOS, TemplateDTO.class);
		return new PageInfo<>(templateDTOList);
	}

	@Override
	public List<TemplateDTO> queryList(Map<String, Object> params) {
		List<TemplateDO> templateDOS = templateDAO.queryList(params);
		List<TemplateDTO> templateDTOList = ObjectClone.cloneList(templateDOS, TemplateDTO.class);
		return templateDTOList;
	}

}
